<template>
  <div>
    <el-dialog :title="info.isAdd?'菜单添加':'菜单编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.pid" placeholder="请选择上级菜单" @change="changePid">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio v-model="form.type" :label="1" disabled>目录</el-radio>
          <el-radio v-model="form.type" :label="2" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标" v-if="form.type==1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" v-else>
          <el-select v-model="form.url" placeholder="请选择菜单图标">
            <el-option
              v-for="item in IndexRoutes"
              :key="item.path"
              :value="'/'+item.path"
            >{{item.meta.title}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添加</el-button>
        <el-button type="primary" v-else @click="modify">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqmenuadd, reqmenuedit, reqmenuinfo } from "../../../request/api";
import { IndexRoutes } from "../../../router/index";
import { successAlert } from "../../../utils/alert";
export default {
  components: {},
  computed: {
    ...mapGetters({}),
  },

  mounted() {},
  props: ["info", "list"],
  data() {
    return {
      //路由集合
      IndexRoutes,
      form: {
        pid: "", //顶级0
        title: "",
        icon: "",
        type: "", //1目录 2菜单
        url: "", //地址
        status: "", //1正常  2禁用
      },
      //图标集合
      icons: [
        "el-icon-user-solid",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-s-help",
        "el-icon-camera-solid",
      ],
    };
  },
  methods: {
    //4.点了取消    
    cancel() {
      this.info.isshow = false;
    },
    //弹框消失     //
    closed(){
        if(!this.info.isAdd){
          this.empty()
        }
      },
    //重置form
    empty() {
      this.form = {
        pid: "", //顶级0
        title: "",
        icon: "",
        type: "", //1目录 2菜单
        url: "", //地址
        status: "", //1正常  2禁用
      };
    },
    //点击添加
    add() {
      reqmenuadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功
          //(1)弹成功
          successAlert(res.data.msg);
          //(2)弹框消失
          this.cancel();
          //（3）清空数据
          this.empty();
          //  (4)刷新列表???????
          this.$emit("init");
        }
      });
    },
    changePid() {
      this.form.pid == 0 ? (this.form.type = 1) : (this.form.type = 2);
    },
    //获取一条数据
    getOne(id) {
      reqmenuinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
        }
        //补id
        this.form.id=id;
      });
    },
    //点击修改
    modify(){
      reqmenuedit(this.form).then(res=>{
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }

      })
    }
  },
};
</script>
<style lang="less" scoped>
</style>